/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GamePlay;

import FrameWork.BestScore;
import FrameWork.IPlay;
import FrameWork.IState;
import FrameWork.RSFont;
import FrameWork.Rectangle;
import Object.MyObject;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

/**
 *
 * @author ThanhTri
 */
public class State_PlayGame extends IState{
     Sprite m_SBack,m_SChicken,m_SCart,m_SEggs,m_SBreakEgg,m_SButton,m_SButton2;
     MyObject m_Cart;
     Vector m_Chickens;
     Vector m_Eggs;
     Random m_Rand;
     int m_Score,m_ScoreDislay;
     int m_IndexButton;
     float m_SaveTimeEggs,m_TimeEggs,m_SaveTimeScore;
     float m_TimeGame;
     boolean m_iPause;
     public State_PlayGame(IPlay _Play){
        super(_Play);
        m_IDState = Global.STATE_MAINGAME ;
    }
    
    public void Init (){
        super.Init();
        try {
            m_SBack = new Sprite (Image.createImage("/image/Fram.png"),
                            240, 320);
            m_SChicken = new Sprite (Image.createImage("/image/chicken.png"),
                            50, 50);
            m_SCart = new Sprite (Image.createImage("/image/cart.png"),
                            64, 44);
            m_SEggs = new Sprite (Image.createImage("/image/eggs.png"),
                            18, 24);
            m_SBreakEgg = new Sprite (Image.createImage("/image/egg.png"),
                            60, 36);
            m_SButton = new Sprite (Image.createImage("/image/bt.png"),
                            64, 64);
            m_SButton2 = new Sprite (Image.createImage("/image/bt2.png"),
                            74, 74);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        m_Cart = new MyObject(88, 270, 64, 10, Global.ID_CART);
        m_Chickens = new Vector();
        m_Chickens.addElement(new MyObject(10, 111, 50, 50, Global.ID_CHICKEN));
        m_Chickens.addElement(new MyObject(10+42,42 , 50, 50, Global.ID_CHICKEN));
        m_Chickens.addElement(new MyObject(10+84, 104, 50, 50, Global.ID_CHICKEN));
        m_Chickens.addElement(new MyObject(10+126, 171, 50, 50, Global.ID_CHICKEN));
        m_Chickens.addElement(new MyObject(10+168, 70, 50, 50, Global.ID_CHICKEN));
        
        m_Eggs = new Vector();
        m_Rand = new Random();
        m_SaveTimeEggs =0;
        m_TimeEggs = (100.0f+m_Rand.nextInt(400))/500.0f;
        m_Score =0;
        m_SaveTimeScore =0;
        m_ScoreDislay = 0;
        m_TimeGame = 60;
        m_iPause = false;
        m_IndexButton =0;
    }
    
    public void Update (float _Time){
        if (!m_iPause) {            
            UpdateTimeGame(_Time);
            UpdateChickens(m_Chickens, _Time);
            UpdateEggs(m_Eggs, _Time);
            DownEggs(m_Chickens, m_Eggs, _Time);
            Collision(m_Eggs, m_Cart);
            UpdateScoreDislay(m_Score, m_ScoreDislay, _Time);
            
        } else {
        }
    }
    public void PointerPressed(int x, int y){
       if (m_iPause==false) {
           Rectangle rp = new Rectangle(0, 0, 40, 40);
           if(rp.Intersects(x, y)) m_iPause = true;
        } else {
           Rectangle rm = new Rectangle(136, 128, 64, 64);
           if(rm.Intersects(x, y)) m_Play.setNextState(new State_Start(m_Play));
           
           Rectangle rt= new Rectangle(40, 128, 64, 64);
           if(rt.Intersects(x, y)){
               if (m_TimeGame>=0) {
                   m_iPause = false;
               } else {
                   m_iPause = false;
                    Retry();
                }
           }
            
        }
    }
    public void KeyPressed (int _Keycode){        
        System.out.println(_Keycode);
        if (m_iPause==false) {
             // Left
            if (_Keycode == -3 || _Keycode == 52){
                m_Cart.m_X-=42;
                if(m_Cart.m_X<=0)m_Cart.m_X+=42;
            }
            //Right
            if (_Keycode == -4 || _Keycode == 54){
                m_Cart.m_X+=42;
                if(m_Cart.m_X>=176)m_Cart.m_X-=42;
            }    
            if (_Keycode == -5 || _Keycode ==53){
                m_iPause = true;
            }
        }else{
             // Left
            if (_Keycode == -3 || _Keycode == 52){
                m_IndexButton= (m_IndexButton-1)%2;
            }
            //Right
            if (_Keycode == -4 || _Keycode == 54){
                m_IndexButton= (m_IndexButton+1)%2;
            }    
            if (_Keycode == -5 || _Keycode ==53){
                if(m_IndexButton==0){
                   if (m_TimeGame>=0) {
                        m_iPause = false;
                   } else {
                       m_iPause = false;
                        Retry();
                    }                    
                }else{
                    m_Play.setNextState(new State_Start(m_Play));
                }
                
            }
        }
           
    }
    
    public void Render (){        
        super.Render();
        m_Graphics.setColor(200, 200, 200);
        m_Graphics.fillRect(0, 0, 240, 320);
        m_SBack.paint(m_Graphics);        
        
        DrawChickens(m_Chickens, m_SChicken);
        DrawEggs(m_Eggs, m_SEggs,m_SBreakEgg);
        DrawCart(m_Cart, m_SCart);
        DrawScore(m_ScoreDislay);
        DrawBestScore(BestScore.get().m_Score);
        DrawTime((int)m_TimeGame);
        
        if (m_iPause==false) {
            DrawButtonPause(m_SButton);
        } else {
            DrawIndexButton(m_SButton2, m_IndexButton);
            if (m_TimeGame>=0) {
                DrawButtonPlay(m_SButton);
            } else {
                DrawButtonRetry(m_SButton);
            }
        }
        
    }    
   
    public void Destroy (){

    }
    public void DrawCart (MyObject _Cart,Sprite _SCart){
        _SCart.setPosition((int)_Cart.m_X,(int) _Cart.m_Y);
        _SCart.paint(m_Graphics);
    }
    public void DrawChickens (Vector _Chickens,Sprite _SChickens){
        for (int i = 0; i < _Chickens.size();i++) {
            MyObject _obj = (MyObject)_Chickens.elementAt(i);
            _SChickens.setFrame(_obj.m_CurFrame);
            _SChickens.setPosition((int)_obj.m_X,(int) _obj.m_Y);
            _SChickens.paint(m_Graphics);
        }
    }
    public void DrawEggs (Vector _Eggs,Sprite _SEggs,Sprite _SBreakEggs){
        for (int i = 0; i < _Eggs.size();i++) {
            MyObject _obj = (MyObject)_Eggs.elementAt(i);
            if(_obj.m_Status== Global.STT_ACTIVE){
                _SEggs.setFrame(_obj.m_CurFrame);
                _SEggs.setPosition((int)_obj.m_X, (int)_obj.m_Y);
                _SEggs.paint(m_Graphics);
            }else{
                if(_obj.m_CurFrame!=3){
                    _SBreakEggs.setPosition((int)_obj.m_X-21, 280);
                    _SBreakEggs.paint(m_Graphics);
                }
            }
            
        }
    }
    public void UpdateEggs(Vector _Eggs,float _Time){
        for (int i = 0; i < _Eggs.size();) {
            MyObject _obj = (MyObject)_Eggs.elementAt(i);
            _obj.UpdateEggs(_Time);
            if (_obj.m_Status==Global.STT_ACTIVE) {
                _obj.m_Y += 200*_Time;
                if(_obj.m_Y>=280)_obj.m_Status = Global.STT_BREAK;
            }            
            if (_obj.m_Status==Global.STT_DIE) {
                _Eggs.removeElementAt(i);
            }else i++;
        }
    }
    public void UpdateChickens(Vector _Chickens,float _Time){
        for (int i = 0; i < _Chickens.size();i++) {
            MyObject _obj = (MyObject)_Chickens.elementAt(i);
            _obj.Update(_Time);
        }
    }
    public void DownEggs(Vector _Chicken,Vector _Eggs,float _Time){
        m_SaveTimeEggs+=_Time;
        if (m_SaveTimeEggs>=m_TimeEggs) {
            m_SaveTimeEggs =0;
            m_TimeEggs = (100.0f+m_Rand.nextInt(900))/1000.0f;            
            MyObject obj = (MyObject)_Chicken.elementAt(m_Rand.nextInt(5));
            int _e = 3;
            
            if(m_Rand.nextInt(5)<4){
                _e = 0;
                if(m_Rand.nextInt(5)<2){
                    _e =1;
                    if(m_Rand.nextInt(3)<1){
                        _e=2;
                    }
                }
            }
            
            m_Eggs.addElement(new MyObject((int)obj.m_X+18,(int)obj.m_Y+20, 18, 24,
                    Global.ID_EGG, _e));
            
        }
    }
    public void Collision(Vector _Eggs,MyObject _Cart){
        for (int i = 0; i < _Eggs.size();) {
            MyObject _obj = (MyObject)_Eggs.elementAt(i);
            if(_obj.m_Status==0){
                if(m_Cart.Rect().Intersects(_obj.Rect())){
                switch(_obj.m_CurFrame){
                    case 0:
                        m_Score+=5;
                        break;
                    case 1:
                        m_Score+=15;
                        break;
                    case 2:
                        m_Score+=30;
                        break;
                    case 3:
                        m_Score-=50;
                        break;
                }
                 System.out.println("Score :"+ m_Score );  
                _Eggs.removeElementAt(i);
                continue;
                }            
            }
            i++;
        }
    }
    public void DrawScore (int _Score){
        RSFont.getRS().getLarge().DrawString(" "+_Score, 98, 12, m_Graphics);
    }
    public void DrawTime (int _Time){
        RSFont.getRS().getLarge().DrawString(" "+_Time, 48, 12, m_Graphics);
    }
    public void DrawIndexButton (Sprite _SButton2, int _Index){
       if(_Index ==0){
            _SButton2.setPosition(35, 123);
            _SButton2.setFrame(0);
            _SButton2.paint(m_Graphics);
       }else{
            _SButton2.setPosition(131, 123);
            _SButton2.setFrame(0);
            _SButton2.paint(m_Graphics);
       }
        
        
    }
    public void UpdateScoreDislay (int _Score,int _ScoreDislay,float _Time){
        if(_Score!=_ScoreDislay){
            m_SaveTimeScore+=_Time;
            if (m_SaveTimeScore>0.01f) {
                m_SaveTimeScore -=0.01f ;
                if(_Score>_ScoreDislay){
                   m_ScoreDislay++; 
                }else{
                    m_ScoreDislay--;
                }               
            }
        }
    }
    public void DrawBestScore (int _Score){
        RSFont.getRS().getLarge().DrawString(" "+_Score, 190, 12, m_Graphics);
    }
    public void DrawButtonPause (Sprite _Spause){
        _Spause.setPosition(0, 0);
        _Spause.setFrame(1);
        _Spause.paint(m_Graphics);
    }
    public void DrawButtonPlay (Sprite _Spause){
        _Spause.setPosition(40, 128);
        _Spause.setFrame(0);
        _Spause.paint(m_Graphics);
        _Spause.setPosition(136, 128);
        _Spause.setFrame(3);
        _Spause.paint(m_Graphics);
    }
    public void DrawButtonRetry (Sprite _Spause){
        _Spause.setPosition(40, 128);
        _Spause.setFrame(2);
        _Spause.paint(m_Graphics);
        _Spause.setPosition(136, 128);
        _Spause.setFrame(3);
        _Spause.paint(m_Graphics);
    }
    public void UpdateBestScore (int _Score,int _BestScore){
        if(_BestScore<_Score)BestScore.get().m_Score=_Score;
    }
    public void UpdateTimeGame (float _Time){
        m_TimeGame-=_Time;
        if(m_TimeGame<0){
            m_iPause = true;
            UpdateBestScore(m_Score, BestScore.get().m_Score);
        }
    }
    public void Retry (){
        m_SaveTimeEggs =0;
        m_TimeEggs = (100.0f+m_Rand.nextInt(400))/500.0f;
        m_Score =0;
        m_SaveTimeScore =0;
        m_ScoreDislay = 0;
        m_TimeGame = 60;
        m_iPause = false;
        m_Eggs.removeAllElements();
    }
}
